package com.joekai.view_demo.zdy_progress_bar;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.joekai.view_demo.R;
import com.joekai.view_demo.utils.PxUtils;

/**
 * 自定义进度条
 *
 * @author joekai
 */
public class ZdyProgressBar extends View {
    //总进度条颜色
    private int mOuterColor = Color.RED;
    //进行中颜色
    private int mInnerColor = Color.BLUE;
    //进度条宽度
    private float mBorderWidget = 20;
    private String mProgressText = "0%";
    private int mProgressTextSize = 20;
    private int mProgressTextColor = Color.RED;
    private Paint mOuterPaint;
    private Paint mInnerPaint;
    private Paint mTextPaint;
    private int mStartAngle = 90;
    private int mEndAngle = 360;
    //画布区域
    private RectF rectF;
    private int mMaxProgress = 100;
    private int mProgress = 150;
    private int mType;
    private static final int circular = 0;

    public enum ProgressType {
        CIRCULAR,
        Line;
    }

    public ZdyProgressBar(Context context) {
        this(context, null);
    }

    public ZdyProgressBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZdyProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public ZdyProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initAttr(context, attrs);
        initPaint();
    }

    //获取自定义属性
    private void initAttr(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ZdyProgressBar);
        mProgressText = ta.getString(R.styleable.ZdyProgressBar_progressText);
        if (TextUtils.isEmpty(mProgressText)) {
            mProgressText = "0%";
        }
        mProgressTextColor = ta.getColor(R.styleable.ZdyProgressBar_progressTextColor, mProgressTextColor);
        mProgressTextSize = ta.getDimensionPixelSize(R.styleable.ZdyProgressBar_progressTextSize, PxUtils.sp2px(mProgressTextSize));
        mBorderWidget = ta.getDimension(R.styleable.ZdyProgressBar_progressBorderWidget, mBorderWidget);
        mOuterColor = ta.getColor(R.styleable.ZdyProgressBar_outerProgressColor, mOuterColor);
        mInnerColor = ta.getColor(R.styleable.ZdyProgressBar_innerProgressColor, mInnerColor);
        mType = ta.getInt(R.styleable.ZdyProgressBar_progressType, circular);
        ta.recycle();
    }

    private void initPaint() {
        mOuterPaint = new Paint();
        //取消锯齿
        mOuterPaint.setAntiAlias(true);
        mOuterPaint.setColor(mOuterColor);
        mOuterPaint.setStrokeWidth(mBorderWidget);
        //设置圆弧Style
        mOuterPaint.setStyle(Paint.Style.STROKE);
        //设置起始，结束圆角
        mOuterPaint.setStrokeCap(Paint.Cap.ROUND);

        mInnerPaint = new Paint();
        //取消锯齿
        mInnerPaint.setAntiAlias(true);
        mInnerPaint.setColor(mInnerColor);
        mInnerPaint.setStrokeWidth(mBorderWidget);
        //设置圆弧Style
        mInnerPaint.setStyle(Paint.Style.STROKE);
        //设置起始，结束圆角
        mInnerPaint.setStrokeCap(Paint.Cap.ROUND);

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(mProgressTextSize);
        mTextPaint.setColor(mProgressTextColor);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获取布局大小
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);


        //设置大小
        switch (MeasureSpec.getMode(heightMeasureSpec)) {
            case MeasureSpec.AT_MOST:
                Rect bounds = new Rect();
                mTextPaint.getTextBounds(mProgressText, 0, mProgressText.length(), bounds);
                height = bounds.height() + getPaddingTop() + getPaddingBottom();
                break;
            default:
                break;
        }
        if (mType == circular) {
            setMeasuredDimension(width > height ? width : height, width > height ? width : height);
        } else {
            setMeasuredDimension(width, height);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mType == circular) {
            //画外圆
            rectF = new RectF(mBorderWidget / 2, mBorderWidget / 2, getWidth() - mBorderWidget / 2, getHeight() - mBorderWidget / 2);
            canvas.drawArc(rectF, mStartAngle, mEndAngle, false, mOuterPaint);
            if (mProgress > mMaxProgress) {
                mProgress = mMaxProgress;
            }

            float sweep = (float) mProgress / mMaxProgress;
            canvas.drawArc(rectF, mStartAngle, mEndAngle * sweep, false, mInnerPaint);
            int pro = (int) (sweep * 100);
            mProgressText = pro + "%";
            int textWidget = (int) mTextPaint.measureText(mProgressText);
            int dx = getWidth() / 2 - textWidget / 2;
            // 计算基线
            //获取文字排版信息
            Paint.FontMetricsInt fontMetricsInt = mTextPaint.getFontMetricsInt();
            int dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom;
            //基线
            int baseLine = getHeight() / 2 + dy;
            canvas.drawText(mProgressText, dx, baseLine, mTextPaint);
        } else {
            if (mProgress > mMaxProgress) {
                mProgress = mMaxProgress;
            }

            float sweep = (float) mProgress / mMaxProgress;
            int pro = (int) (sweep * 100);
            mProgressText = pro + "%";
            int textWidth = (int) mTextPaint.measureText(mProgressText);
            canvas.drawLine(mBorderWidget / 2, (getHeight()) / 2, getWidth() - mBorderWidget / 2 - textWidth - 30, (getHeight()) / 2, mOuterPaint);// 画线

            canvas.drawLine(mBorderWidget / 2, (getHeight()) / 2, (getWidth() - mBorderWidget / 2 - textWidth - 30) * sweep, (getHeight()) / 2, mInnerPaint);// 画线
//获取文字排版信息
            Paint.FontMetricsInt fontMetricsInt = mTextPaint.getFontMetricsInt();
            int dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom;
            int baseLine = getHeight() / 2 + dy;
            canvas.drawText(mProgressText, getWidth() - mBorderWidget / 2 - textWidth, baseLine, mTextPaint);
        }
    }

    public synchronized void setMaxProgress(int mMaxProgress) {
        this.mMaxProgress = mMaxProgress;
    }

    public synchronized void setProgress(int mProgress) {
        this.mProgress = mProgress;
        //不断绘制
        invalidate();
    }

    public synchronized void setAnimatorProgress(int step) {
        //属性动画
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, step);
        valueAnimator.setDuration(1000);
        valueAnimator.addUpdateListener(valueAnimator1 -> {
            float mProgress = (float) valueAnimator1.getAnimatedValue();
            setProgress((int) mProgress);
        });
        valueAnimator.start();
    }

    public synchronized void setProgressType(ProgressType type) {
        if (type.equals(ProgressType.CIRCULAR)) {
            this.mType = 0;
        } else {
            this.mType = 1;
        }
    }
}
